﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using SqliteWrapper.Procedures;
using SqliteWrapper;
using BartonExtension;

namespace SqliteWrapper
{
    public class NonQueryWrapper
    {
        public string DBPath { get; set; }

        public static NonQueryWrapper Create(string path)
        {
            return new NonQueryWrapper() { DBPath = path };
        }

        //Album Store Procedure
        public SqliteError AddAlbum(string tit,string author, string desc,long cid,string bookSrcs,string thumb)
        {
            //Set books
            List<Book> books = new List<Book>();
            int i = 1;
            foreach (string bookSrc in bookSrcs.Split(new string[]{";"},StringSplitOptions.RemoveEmptyEntries))
            {
                Book bk = new Book()
                {
                    Title = "{0}--{1}".FormatWith(tit,i),
                    SourcePath = bookSrc,
                    Thumbnail = thumb,
                    Desc = desc,
                    AlbumId = -1,
                    CategoryId = cid,
                    CreateTimeTag = DateTime.Now.ToString("yyyy-MM-dd")
                };
                books.Add(bk);
                i++;
            }

            Album album = new Album()
            {
                Title = tit,
                Desc = desc,
                Thumbnail = thumb,
                CreateTimeTag = DateTime.Now.ToString("yyyy-MM-dd"),
                BookList = books
            };
            Proc_AddAlbum procedure = new Proc_AddAlbum();
            return procedure.doProcedure(album,DBPath);
        }

        public SqliteError DeleteAlbum(bool deleteBooks, long aid)
        {
            Album album = new Album() { ID = aid };
            Proc_DeleteAlbum procedure = new Proc_DeleteAlbum(deleteBooks);
            return procedure.doProcedure(album, DBPath);
        }

        public SqliteError UpdateAlbum(long albumId, string name,string author,string desc,string thumb, bool reIssue)
        {
            Album album = new Album() { ID = albumId,Title = name,Desc = desc,Thumbnail = thumb };
            Proc_UpdateAlbum procedure = new Proc_UpdateAlbum(reIssue);
            return procedure.doProcedure(albumId, DBPath);
        }

        //Book Store Procedure
        public SqliteError AddBook(string tit,string author, string sPath, string desc, long aid, long cid,string thumb)
        {
            Book book = new Book()
            {
                Title = tit,
                Author = author,
                SourcePath = sPath,
                Thumbnail = thumb,
                Desc = desc,
                AlbumId = aid,
                CategoryId = cid,
                CreateTimeTag = DateTime.Now.ToString("yyyy-MM-dd"),
                Version = 1.0f
            };
            Proc_InsertBook procedure = new Proc_InsertBook();
            return procedure.doProcedure(book,DBPath);
        }

        public SqliteError BatchImportBooks(int categoryId, List<string> tit, List<string> desc, List<string> srcPath,List<string> thumbs)
        {
            List<Book> bks_will_import = new List<Book>();
            tit.ForEach(t =>
            {
                int index = tit.IndexOf(t);
                Book bk = new Book()
                {
                    Title = t,
                    Desc = desc[index],
                    SourcePath = srcPath[index],
                    Thumbnail = thumbs[index],
                    AlbumId = -1,
                    CategoryId = categoryId,
                    CreateTimeTag = DateTime.Now.ToString("yyyy-MM-dd")
                };
                bks_will_import.Add(bk);
            });
            Proc_BatchInsertBooks procedure = new Proc_BatchInsertBooks();
            return procedure.doProcedure(bks_will_import, DBPath);
        }

        public SqliteError DeleteBook(long id)
        {
            Book book = new Book() { ID = id };
            Proc_DeleteBook procedure = new Proc_DeleteBook();
            return procedure.doProcedure(book, DBPath);
        }

        public SqliteError UpdateBook(string tit,string author, string sPath, string desc, long aid, long cid,string thumb,double version)
        {
            Book book = new Book() { SourcePath = sPath, Title = tit,Author = author, Desc = desc, AlbumId = aid, CategoryId = cid, Thumbnail = thumb,Version = (float)version };
            Proc_UpdateBook procedure = new Proc_UpdateBook();
            return procedure.doProcedure(book, DBPath);
        }

        //Category Store Procedure
        public SqliteError AddCategory(string tit, string desc)
        {
            Category category = new Category()
            {
                Title = tit,
                Desc = desc,
                CreateTimeTag = DateTime.Now.ToString("yyyy-MM-dd")
            };
            Proc_AddCategory procedure = new Proc_AddCategory();
            return procedure.doProcedure(category,DBPath);
        }

        public SqliteError DeleteCategory(long cid, bool deleteBook)
        {
            Category category = new Category() { ID = cid };
            Proc_DeleteCategory procedure = new Proc_DeleteCategory(deleteBook);
            return procedure.doProcedure(category, DBPath);
        }

        public SqliteError UpdateCategory(long cid, string newName,string newDesc)
        {
            Category category = new Category() { ID = cid, Title = newName,Desc = newDesc };
            Proc_UpdateCategory procedure = new Proc_UpdateCategory();
            return procedure.doProcedure(category, DBPath);
        }

        public SqliteError DownloadBook(long bid)
        {
            Book bk = new Book() { ID = bid };
            Proc_DownloadBook procedure = new Proc_DownloadBook();
            return procedure.doProcedure(bk, DBPath);
        }

        public SqliteError AddComment(Comment comment)
        {
            Proc_AddComment procedure = new Proc_AddComment();
            return procedure.doProcedure(comment, DBPath);
        }

        public SqliteError RateBook(string json)
        {
            Proc_RateBook procedure = new Proc_RateBook();
            UserRateData rate = json.FromJson<UserRateData>();
            return procedure.doProcedure(rate, DBPath);
        }

        public SqliteError Excute(string nonQuery)
        {
            NonQueryCommon excuter = new NonQueryCommon();
            SqliteError error = excuter.Query(nonQuery,DBPath);
            return error;
        }
    }
}
